package com.huluobo.a2207douyindemo.mine;

import android.util.Log;

import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.adapter.LikeCollectAdapter;
import com.huluobo.a2207douyindemo.databinding.FragmentCollectBinding;
import com.huluobo.base.mvvm.BaseMVVMFragment;

public class CollectFragment extends BaseMVVMFragment<FragmentCollectBinding, MineViewModel> {

    private LikeCollectAdapter likeCollectAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_collect;
    }

    @Override
    protected void initData() {
        vm.getCollectVideos();

        vm.getCollectVideosSuccess.observe(this, videoBeans -> {
            likeCollectAdapter.getData().clear();
            likeCollectAdapter.getData().addAll(videoBeans);
            likeCollectAdapter.notifyDataSetChanged();
        });

        vm.getCollectVideosFailed.observe(this, s -> {
            Log.i("TagA", "查询收藏视频失败:" + s);
        });

        vm.deleteCollectSuccess.observe(this, s -> {
            ToastUtils.showLong(s);
            vm.getCollectVideos();
        });

        vm.deleteCollectFailed.observe(this, s -> {
            Log.i("TagA", "删除收藏失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.collectRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        likeCollectAdapter = new LikeCollectAdapter();
        binding.collectRv.setAdapter(likeCollectAdapter);

        likeCollectAdapter.addChildClickViewIds(R.id.like_collect_delete_btn);
        likeCollectAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.like_collect_delete_btn) {
                vm.deleteCollectVideo(likeCollectAdapter.getData().get(position));
            }
        });
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}